<template>
  <div class="s-InteractionConsult">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm" :size="formSize" status-icon>
      <el-form-item label="标      题" prop="title">
        <el-input v-model="ruleForm.title" placeholder="您要说明问题的主题" />
      </el-form-item>
      <el-form-item label="问题详情" prop="desc">
        <el-input type="textarea" v-model="ruleForm.desc" placeholder="详细说明您提出的问题"></el-input>
      </el-form-item>
      <el-form-item label="姓      名" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请使用已注册的用户名"></el-input>
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="ruleForm.email" placeholder="请正确填写邮箱地址，我们将把回复发送到您的邮箱"></el-input>
      </el-form-item>
      <el-form-item label="联系方式" prop="tel">
        <el-input v-model="ruleForm.tel" placeholder="请正确填写固定电话或者手机号码，以便于回复和沟通"></el-input>
      </el-form-item>
      <el-form-item>
        <p class="submit">
          <el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button>
        </p>
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup>
import { ref, reactive } from "vue";
const formSize = ref("default");
const ruleFormRef = ref();
const ruleForm = reactive({
  // title: "",
});
const validatorPhone = function (rule, value, callback) {
  console.log(value);

  if (!value) {
    callback(new Error("手机号不能为空"));
  } else if (!/^1\d{10}$/.test(value)) {
    callback(new Error("手机号格式错误"));
  } else {
    callback();
  }
};
const rules = reactive({
  title: [
    { required: true, message: "请输入标题", trigger: "blur" },
    { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" },
  ],
  desc: [
    { required: true, message: "请详细说明您提出的问题", trigger: "blur" },
    { min: 3, max: 500, message: "长度在 3 到 500 个字符", trigger: "blur" },
  ],
  name: [{ required: true, message: "请使用已注册的用户名", trigger: "blur" }],
  email: [
    { required: true, message: "请输入邮箱地址", trigger: "blur" },
    { type: "email", message: "请输入正确的邮箱地址", trigger: ["blur", "change"] },
  ],
  tel: [{ required: true, validator: validatorPhone, trigger: "blur" }],
});
const submitForm = async formEl => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log(ruleForm);
    } else {
      console.log("error submit!", fields);
    }
  });
};
</script>
